package com.company;

import com.company.transport.FileReceiver;
import com.company.transport.Receiver;
import com.company.utils.CloseUtils;
import com.company.utils.ZipUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;

import static com.sun.tools.doclets.formats.html.markup.HtmlStyle.header;

public class Main {

    public static void main(String[] args) {
        // write your code here
        String host = args[0];
        int port = 8888;

        if (args.length == 1) {
            // i think user want to pull file;
            Socket socket = null;
            OutputStream outputStream = null;
            InputStream inputStream = null;
            try {
                socket = new Socket(host, port);
                outputStream = socket.getOutputStream();
                //write header;
                writeHeader("pull",outputStream);

                inputStream = socket.getInputStream();

                Receiver receiver = new FileReceiver();
                receiver.receive(new DataInputStream(inputStream));

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("io异常,检查服务器是否启动,或者ip是否输入正确");
            } finally {
                CloseUtils.closeIO(inputStream);
            }

        } else {
            for (int i = 1; i < args.length; i++) {
                String arg = args[i];
                System.out.println("将" + arg + "推送到设备");
                OutputStream outputStream = null;
                FileInputStream fileInputStream = null;
                Socket socket = null;
                try {
                    socket = new Socket(host, port);
                    File file = new File(arg);
                    String fileName = file.getName();
                    fileInputStream = new FileInputStream(file);
                    outputStream = socket.getOutputStream();
                    //write header
                    writeHeader(fileName, outputStream);
                    //write body;

                    int len;
                    byte[] b = new byte[1024 * 5];
                    long length = file.length();
                    long currentLength = 0;
                    long lastCurrentProgress = 0;
                    while ((len = fileInputStream.read(b)) != -1) {
                        outputStream.write(b, 0, len);
                        currentLength += len;
                        float percent = currentLength * 1.0f / length;
                        int currentProgress = (int) (percent * 100);
                        if (lastCurrentProgress + 5 < currentProgress || currentProgress == 100) {
                            System.out.println(fileName + currentProgress + "%");
                            lastCurrentProgress = currentProgress;
                        }

                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    System.out.println(arg + "推送到手机端失败!请重试");
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                    System.out.println("无法找到主机地址");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.println("io读取失败,检查服务是否启动");
                } finally {
                    CloseUtils.closeIO(outputStream);
                    CloseUtils.closeIO(fileInputStream);
                    CloseUtils.closeIO(socket);
                }


            }

        }
    }


    public static String readHeader(InputStream inputStream) throws IOException {
        byte[] headerCache = new byte[512];
        inputStream.read(headerCache, 0, headerCache.length);
        return new String(headerCache, Charset.forName("utf-8")).trim();
    }


    public static void writeHeader(String header, OutputStream outputStream) throws IOException {
        byte[] bytes = header.getBytes(Charset.forName("utf-8"));
        byte[] placeHolder = new byte[512 - bytes.length];
        outputStream.write(bytes);
        outputStream.write(placeHolder);

    }


    public static void writeDataToFile(InputStream inputStream, FileOutputStream outputStream) throws IOException {

        int len;
        byte[] cache = new byte[1024 * 5];
        while ((len = inputStream.read(cache)) != -1) {
            outputStream.write(cache, 0, len);
        }

    }
}
